<?php

class spPageEdit extends spPage
{
	private $data;
	
	public function setData( $data )
	{
		$this->data = $data;
	}
	
	public function render()
	{
		$d = $this->data;

		parent::spRenderDefaults();
		parent::spRenderHeading( $d["heading"] );
		
		$formBuilder = $d["formbuilder"];
		$formBuilder->setDatabase( $d["database"] );
		$formBuilder->setFormButtonsBottom(new spFormButtons(array(
			array( 
				"type"		=> "submit",
				"value" 	=> isset($d["savetext"])?$d["savetext"]:"Save", 
				"name" 		=> $d["ajax"]
			)
		)));	
		
		$formBuilder->addHiddenField( $d["id_name"], $d["id"] );
		
		parent::addChild( $formBuilder );
		
		parent::render();
	}
}

function spAjaxEdit( $database, $redirect )
{
	$_REQUEST = unMagicQuotify($_REQUEST);

	$database->updateFromRequest();
	if ( !$database->save() )
	{
		$errors = $database->getFormErrors();
		$json = array(
			"formerrors" => $errors
		);
		echo json_encode( $json );
	}
	else
	{
		echo '{"redirect":"'.$redirect.'"}';
	}	
}



?>